/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author Sik
 */
@Entity
@Table(name = "zone")
@XmlRootElement
@NamedQueries(
{
    @NamedQuery(name = "Zone.findAll", query = "SELECT z FROM Zone z"),
    @NamedQuery(name = "Zone.findByIdZone", query = "SELECT z FROM Zone z WHERE z.idZone = :idZone"),
    @NamedQuery(name = "Zone.findByDesignation", query = "SELECT z FROM Zone z WHERE z.designation = :designation"),
    @NamedQuery(name = "Zone.findByDetails", query = "SELECT z FROM Zone z WHERE z.details = :details")
})
public class Zone implements Serializable
{
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idZone")
    private Integer idZone;
    @Basic(optional = false)
    @Column(name = "designation")
    private String designation;
    @Column(name = "details")
    private String details;
    @Lob
    @Column(name = "commentaires")
    private String commentaires;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "zoneidZone")
    private List<Epingle> epingleList = new ArrayList<Epingle>();
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "zone")
    private List<Attribution> attributionList = new ArrayList<Attribution>();
    @JoinColumn(name = "Carte_idCarte", referencedColumnName = "idCarte")
    @ManyToOne(optional = false)
    private Carte carteidCarte;
    
    public Zone()
    {
    }

    public Zone(Integer idZone)
    {
        this.idZone = idZone;
    }

    public Zone(Integer idZone, String designation)
    {
        this.idZone = idZone;
        this.designation = designation;
    }

    public Integer getIdZone()
    {
        return idZone;
    }

    public void setIdZone(Integer idZone)
    {
        this.idZone = idZone;
    }

    public String getDesignation()
    {
        return designation;
    }

    public void setDesignation(String designation)
    {
        this.designation = designation;
    }

    public String getDetails()
    {
        return details;
    }

    public void setDetails(String details)
    {
        this.details = details;
    }

    public String getCommentaires()
    {
        return commentaires;
    }

    public void setCommentaires(String commentaires)
    {
        this.commentaires = commentaires;
    }

    public Carte getCarteidCarte()
    {
        return carteidCarte;
    }

    public void setCarteidCarte(Carte carteidCarte)
    {
        this.carteidCarte = carteidCarte;
    }
    
    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (idZone != null ? idZone.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Zone))
        {
            return false;
        }
        Zone other = (Zone) object;
        if ((this.idZone == null && other.idZone != null) || (this.idZone != null && !this.idZone.equals(other.idZone)))
        {
            return false;
        }
        return true;
    }
    
    public String xmlParse(boolean displayPins)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<zone>");
            sb.append("<idZone>").append(this.getIdZone()).append("</idZone>");
            sb.append("<designation>").append(this.getDesignation()).append("</designation>");
            sb.append("<details>").append(this.getDetails()).append("</details>");
            // TODO inserer places depuis BDD
            sb.append("<places>").append(" ").append("</places>");
            sb.append("<commentaires>").append(this.getCommentaires()).append("</commentaires>");
            sb.append("<path>").append(this.getPath()).append("</path>");
            if(this.attributionList == null || this.attributionList.isEmpty())
            {
                sb.append("<disponibilite>true</disponibilite>");
            }
            else
            {
                sb.append("<disponibilite>false</disponibilite>");
                sb.append("<attributions>");
                for(Attribution a : this.attributionList)
                {
                    sb.append("<etudiant>").append(a.getUtilisateur().getNom()).append(" ").append(a.getUtilisateur().getPrenom()).append("</etudiant>");
                }
                sb.append("</attributions>");
            }
            if(displayPins)
            {
                sb.append("<epingles>");
                    for(Epingle e : this.epingleList)
                    {
                        sb.append(e.xmlParse());
                    }
                sb.append("</epingles>");
            }
        sb.append("</zone>");
        
        return sb.toString();
    }
    
    public String getCoords()
    {
        StringBuilder sb = new StringBuilder();
        int count = 0;
        for(Epingle pin : this.epingleList)
        {
            sb.append(pin.getX()).append(",").append(pin.getY());
            if(count <= this.epingleList.size())
            {
                sb.append(",");
            }
        }
        return sb.toString();
    }
    
    public String getPath()
    {
        int i = 0;
        StringBuilder path = new StringBuilder();
        path.append("M ");
        
        for(Epingle e : this.epingleList)
        {
            if(i == 0)
            {
                path.append(e.getX()).append(" ").append(e.getY()).append(" ");
            }
            else
            {
                path.append("L ").append(e.getX()).append(" ").append(e.getY()).append(" ");
            }
            i++;
        }
        path.append("z");
        return path.toString();
    }

    @Override
    public String toString()
    {
        return "Model.Zone[ idZone=" + idZone + " ]";
    }

    @XmlTransient
    public List<Epingle> getEpingleList()
    {
        return epingleList;
    }

    public void setEpingleList(List<Epingle> epingleList)
    {
        this.epingleList = epingleList;
    }

    @XmlTransient
    public List<Attribution> getAttributionList()
    {
        return attributionList;
    }

    public void setAttributionList(List<Attribution> attributionList)
    {
        this.attributionList = attributionList;
    }
}
